package com.jh.user.service;

import com.jh.user.util.CommonsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jh.user.business.CheckUserBusiness;
import com.jh.user.pojo.CheckUser;
import com.jh.user.util.Util;

@Controller
@EnableAutoConfiguration
public class CheckUserService {

	private static final Logger LOG = LoggerFactory.getLogger(CheckUserService.class);

	@Autowired
	private CheckUserBusiness checkUserBusiness;

	@Autowired
	Util util;

	//查询用户信息
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/checkuser/querybyid")
	public @ResponseBody Object queryUserById(@RequestParam(value = "id", defaultValue="1", required=false) long id){
		try {
			CheckUser queryUserById = checkUserBusiness.queryUserById(id);
			return CommonsUtil.returnSuccess(queryUserById);
		}catch (Exception e){
			LOG.error("CheckUserService.queryUserById, 查询用户信息异常=" + e);
			return CommonsUtil.returnError("查询用户信息失败！");
		}
	}


	//增加用户信息
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/checkuser/add")
	public @ResponseBody Object adduser(@RequestParam(value = "username") String name,
										@RequestParam(value = "gender", defaultValue="0", required=false) int gender,
										@RequestParam(value = "phone", defaultValue="18300700074", required=false) String phone,
										@RequestParam(value = "userinfo", defaultValue="", required=false) String userinfo){
		try {
			CheckUser checkUser = new CheckUser();
			checkUser.setUserName(name);
			checkUser.setGender(gender);
			checkUser.setPhone(phone);
			checkUser.setUserinfo(userinfo);
			checkUserBusiness.saveCheckUser(checkUser);
			return CommonsUtil.returnSuccess("");
		}catch (Exception e){
			LOG.error("CheckUserService.adduser, 增加用户信息异常=" + e);
			return CommonsUtil.returnError("增加用户信息失败！");
		}
	}



	//修改用户信息
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/checkuser/update")
	public @ResponseBody Object updateuser(@RequestParam(value = "id") long id,
										   @RequestParam(value = "username") String name,
										   @RequestParam(value = "gender", defaultValue="0", required=false) int gender,
										   @RequestParam(value = "phone", defaultValue="18300700074", required=false) String phone,
										   @RequestParam(value = "userinfo", defaultValue="", required=false) String userinfo){
		try {
			CheckUser checkUser = checkUserBusiness.queryUserById(id);
			checkUser.setUserName(name!=null&&!name.equals("")?name:checkUser.getUserName());
			checkUser.setGender(gender!=-1?gender:checkUser.getGender());
			checkUser.setPhone(phone!=null&&!phone.equals("")?phone:checkUser.getPhone());
			checkUser.setUserinfo(userinfo!=null&&!userinfo.equals("")?userinfo:checkUser.getUserinfo());
			checkUserBusiness.saveCheckUser(checkUser);
			return CommonsUtil.returnError("");
		}catch (Exception e){
			LOG.error("CheckUserService.updateuser, 修改用户信息异常=" + e);
			return CommonsUtil.returnError("修改用户信息失败！");
		}
	}


}
